package  samples.d3.color
{
	import com.adobe.kuler.events.GetResultEvent;
	import com.adobe.kuler.params.SearchParams;
	import com.adobe.kuler.swatches.swatch.color.Color;
	import flash.display.MovieClip;
	import com.adobe.kuler.KLibService;
	import flash.display.Sprite;	
	import flash.utils.setInterval;
	import samples.BaseSample;
	
	/**
	 * This is a example of using klib to get swatches from Adobe's kuler API.
	 */
	public class Kuler extends BaseSample
	{		
		private var klib:KLibService;
		public function Kuler() 
		{
			klib = new KLibService("823560452EC50A926575D1F7C5011321"); //Get an API get at http://kuler.adobe.com/api/
			klib.addEventListener( GetResultEvent.GET_RESULTS, onResults);
			
			//Here is a list of example ways to use the kuler API. Just uncomment the one you want to use.
			
			
			//getRecent search example
			//klib.getRecent( 0,1 ); 
			
			
			//getHighestRated search exmaple
			//klib.getHighestRated();
			
			
			//getPopular search example
			//klib.getPopular();
			
			
			//getRandom search example
			//klib.getRandom();
			
			
			//Example of email search
			//var temp:SearchParams = new SearchParams(SearchParams.EMAIL, "joshchernoff@msn.com");			
			//klib.searchSwatches(temp);
			
			
			//Example of tag search
			//var temp:SearchParams = new SearchParams(SearchParams.TAG, "sun");
			//klib.searchSwatches(temp);
			
			//Example of hex search
			//var temp:SearchParams = new SearchParams(SearchParams.HEX, "0xFFCC00");			
			//klib.searchSwatches(temp, 0,50 );
			
			
			//Example of THEME_ID search
			//var temp:SearchParams = new SearchParams(SearchParams.THEME_ID, "376159");			
			//klib.searchSwatches(temp);
			
			//Example of TITLE search
			//var temp:SearchParams = new SearchParams(SearchParams.TITLE, "Lady and the Tramp");			
			//klib.searchSwatches(temp);
			
			//Example of USER_ID search
			//var temp:SearchParams = new SearchParams(SearchParams.USER_ID, "211483");			
			//klib.searchSwatches(temp);		
			
			setInterval( reload, 1000 );
			reload();
		}		
		
		private function reload():void 
		{
			klib.getHighestRated( int( Math.random() * 30000 ) ,1 ); 
		}
		
		private function onResults(e:GetResultEvent):void 
		{
			//trace(e);
			//trace("recordCount", e.results.recordCount)
			//trace("startIndex", e.results.startIndex);
			//trace("itemsPerPage", e.results.itemsPerPage );
			
			
			graphics.clear();
			
			var i:int = 0;
			for each(var swatch:Object in e.results.swatches)
			{
				
				graphics.lineStyle(0, 0, 0);
				
				for ( var j:int = 0; j < swatch.colors.length; j++ )
				{
					
					var color:Color = swatch.colors[ j ];
					graphics.beginFill( int( "0x" + color.swatchHexColor ) );
					graphics.drawRect( j * 100,0, 100,500);
					graphics.endFill();
					
				}
				
			}
				
				
				/*
				
				//Make a blank holder per swatch
				var swatchHolder:Sprite = new Sprite();
				addChild(swatchHolder);
				//place it's x and y
				swatchHolder.y = i++ * 12;
				
				//Fill the colors per swatch
				var j:int = 0;
				for each(var color:Color in swatch.colors)
				{
					swatchHolder.graphics.lineStyle(0,0,0);
					swatchHolder.graphics.beginFill(int("0x"+color.swatchHexColor)); //Get color of each color in this swatch per loop.
					swatchHolder.graphics.drawRect(j++ * 100,0, 100,500);
					swatchHolder.graphics.endFill();
				}		
				*/
			
			center();
			
		}		
	}	
}